package com.csy.tcshop.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 网关跨域有关配置
 */
@Configuration//定义为配置类
public class TcshopCorsConfiguration {
    @Bean//将方法加入spring容器中
    public CorsWebFilter corsWebFilter(){
        //UrlBasedCorsConfigurationSource为基于url路径的跨域配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1、配置跨域设置
        corsConfiguration.addAllowedHeader("*");//设置允许那些头可以跨域
        corsConfiguration.addAllowedMethod("*");//设置允许那些方式可以跨域
        corsConfiguration.addAllowedOrigin("*");//设置允许那些来源可以跨域
        corsConfiguration.setAllowCredentials(true);//设置是否允许携带cooket可以跨域
        source.registerCorsConfiguration("/**",corsConfiguration);//注册跨域的配置,/**代表任何路径
        return new CorsWebFilter(source);
    }
}
